package web_thms_mapper

import (
	"context"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"sync"
	"transport/web/web_thms_model"
	"transport/web/web_vo"
)

type GpsMapper struct {
	DB   *gorm.DB
	Ctx  context.Context
	Lock *sync.RWMutex
}

func (mapper *GpsMapper) Create(m *web_thms_model.YtlGps) (ret *web_thms_model.YtlGps, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) Upsert(m *web_thms_model.YtlGps) (ret *web_thms_model.YtlGps, err error) {
	if err = mapper.DB.WithContext(mapper.Ctx).Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "name"}},
		DoUpdates: clause.AssignmentColumns([]string{"code", "short_name", "code", "addr", "contact",
			"telephone", "short_code", "delivery_addr", "route_id", "route_name", "status", "trustee_id", "trustee_name"}),
	}).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) Update(m *web_thms_model.YtlGps) (ret *web_thms_model.YtlGps, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlGps{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) CreateOrUpdate(m *web_thms_model.YtlGps) (ret *web_thms_model.YtlGps, err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) QueryById(id int64) (m *web_thms_model.YtlGps, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlGps{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) QueryByCode(code string) (m *web_thms_model.YtlGps, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlGps{}).Where("code = ?", code).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) QueryByIds(ids []int64) (m []*web_thms_model.YtlGps, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_thms_model.YtlGps{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *GpsMapper) DeleteById(id int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_thms_model.YtlGps{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *GpsMapper) DeleteByIds(ids []int64) (err error) {
	mapper.Lock.Lock()
	defer mapper.Lock.Unlock()
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_thms_model.YtlGps{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *GpsMapper) Page(pageIndex, pageSize int, customerId, deviceId string) (items []*web_vo.GpsPageRet, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx).Table("ytl_gps").Select("ytl_gps.*,strftime('%Y-%m-%d %H:%M:%f',created_at,'localtime') as f_created_at")
	if customerId != "" {
		table = table.Where("customer_id like ?", "%"+customerId+"%")
	}
	if deviceId != "" {
		table = table.Where("device_id like ?", "%"+deviceId+"%")
	}
	if err := table.Order("created_at desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *GpsMapper) List() (items []*web_thms_model.YtlGps, count int64, err error) {
	mapper.Lock.RLock()
	defer mapper.Lock.RUnlock()
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
